package com.zhentao.dataintegrity.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhentao.dataintegrity.domain.entity.DataTypeConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DataTypeConfigMapper extends BaseMapper<DataTypeConfig> {

    /**
     * 查询所有启用的数据类型配置
     */
    @Select("SELECT * FROM di_data_type_config " +
            "WHERE status = 'ACTIVE' AND is_delete = 0 " +
            "ORDER BY sort_order")
    List<DataTypeConfig> selectActiveConfigs();

    /**
     * 根据类型编码查询配置
     */
    @Select("SELECT * FROM di_data_type_config " +
            "WHERE type_code = #{typeCode} AND is_delete = 0")
    DataTypeConfig selectByTypeCode(@Param("typeCode") String typeCode);

    /**
     * 根据类型编码列表查询配置
     */
    @Select("<script>" +
            "SELECT * FROM di_data_type_config " +
            "WHERE type_code IN " +
            "<foreach item='code' collection='typeCodes' open='(' separator=',' close=')'>" +
            "#{code}" +
            "</foreach>" +
            "AND status = 'ACTIVE' AND is_delete = 0 " +
            "ORDER BY sort_order" +
            "</script>")
    List<DataTypeConfig> selectByTypeCodes(@Param("typeCodes") List<String> typeCodes);

    /**
     * 检查类型编码是否存在
     */
    @Select("SELECT COUNT(*) FROM di_data_type_config " +
            "WHERE type_code = #{typeCode} AND is_delete = 0 " +
            "AND (#{excludeId} IS NULL OR id != #{excludeId})")
    int checkTypeCodeExists(@Param("typeCode") String typeCode, @Param("excludeId") Long excludeId);
}
